namespace SqlQueryBuilder.Clauses
{
    /// <summary>
    /// Update Clause
    /// </summary>
    public class UpdateClause : AbstractValueClause
    {
        #region Properties

        /// <summary>
        /// Gets or sets the field to update.
        /// </summary>
        /// <value>The field.</value>
        public string field { get; set; }

        /// <summary>
        /// Gets or sets the new value of the field.
        /// </summary>
        /// <value>The new value.</value>
        public object newValue { get; set; }

        #endregion
        
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UpdateClause"/> class.
        /// </summary>
        /// <param name="field">The field.</param>
        /// <param name="newValue">The new value.</param>
        public UpdateClause(string field, object newValue)
        {
            this.field = field;
            this.newValue = newValue;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UpdateClause"/> class.
        /// </summary>
        /// <param name="field">The field.</param>
        /// <param name="literal">The literal.</param>
        public UpdateClause(string field, SqlLiteral literal)
        {
            this.field = field;
            newValue = literal;
        }

        #endregion
        
        /// <summary>
        /// Builds the update clause.
        /// </summary>
        /// <returns></returns>
        public string BuildUpdateClause()
        {
            return string.Format("{0} = {1} ", field, FormatSQLValue(newValue));
        }

        /// <summary>
        /// Literals the specified field.
        /// </summary>
        /// <param name="field">The field.</param>
        /// <param name="newValue">The new value.</param>
        /// <returns></returns>
        public static UpdateClause Literal(string field, string newValue)
        {
            return new UpdateClause(field,new SqlLiteral(newValue));
        }
    }
}